<template>
  <div class="div1">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>咨询预约管理</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  <div class="container">
    <!-- 搜索区域 -->
    <el-card class="search-card">
      <el-form :inline="true" class="search-form">
        <el-form-item label="预约用户">
          <el-input size="large" v-model="searchName" clearable @clear="query" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="咨询师">
          <el-input size="large" v-model="searchUserName" clearable @clear="query" placeholder="请输入咨询师" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="large" @click="query" class="bt1">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button type="danger" size="large" class="bt1" @click="removeAll">
            <el-icon><Delete /></el-icon>
            批量删除
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 表格区域 -->
    <el-card class="table-card">
      <el-table
        :data="tableData"
        border
        stripe
        highlight-current-row
        style="width: 100%; border-radius: 8px"
        row-key="id"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="ID" width="85"></el-table-column>
        <el-table-column prop="appointmentTime" label="预约时间"></el-table-column>
        <el-table-column prop="description" label="情况说明"></el-table-column>
        <el-table-column prop="sysUser.username" label="预约用户"></el-table-column>
        <el-table-column prop="counselor.name" label="咨询师"></el-table-column>
        <el-table-column prop="counselorOrder.id" label="订单号"></el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template #default="scope">
            <el-button type="danger" size="small" @click="remove(scope.row)">
              <el-icon><Delete /></el-icon>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <div class="pagination-wrapper" v-if="total != 0">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>
<script setup>
import { onMounted, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search, Delete } from '@element-plus/icons-vue'
import { getCurrentInstance } from 'vue'

const axios = getCurrentInstance().appContext.config.globalProperties.axios
const tableData = ref([])
const currentPage = ref(1)
const pageSize = ref(5)
const size = 'default'
const total = ref(0)
const searchName = ref('')
const searchUserName = ref('')

function query() {
  axios.get(
    `appointment/page?sysUser.username=${searchName.value}&counselor.name=${searchUserName.value}&pageNum=${currentPage.value}&pageSize=${pageSize.value}`,
  ).then(resp => {
    tableData.value = resp.data.list;
    total.value = resp.data.total
  })
}

const handleSizeChange = (val) => {
  pageSize.value = val;
  query();
}
const handleCurrentChange = (val) => {
  currentPage.value = val
  query();
}
const remove = (row) => {
  ElMessageBox.confirm(
      '此操作将永久删除该文件, 是否继续?',
      'Warning', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )
    .then(() => {
      axios.delete(`appointment/${row.id}`).then((resp) => {
        if (resp.data.code == 200) {
          ElMessage({
            message: '删除成功',
            type: 'success',
          })
        } else {
          ElMessage.error('删除失败')
        }
      }).finally(() => {
        query();
      });
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val.map(item => item.id)
}
const removeAll = () => {
  if (multipleSelection.value.length === 0) {
    ElMessage.warning('请先选择要删除的数据')
    return
  }
  ElMessageBox.confirm(
      '此操作将永久删除选中的文件, 是否继续?',
      'Warning', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )
    .then(() => {
      axios.delete(`appointment`, {
        params: {
          ids: multipleSelection.value.join(',')
        }
      }).then(resp => {
        if (resp.code == 200) {
          ElMessage({
            message: '删除成功',
            type: 'success',
          })
        } else {
          ElMessage.error('删除失败')
        }
      }).finally(() => {
        query();
      });
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}
onMounted(() => {
  query();
})
</script>
<style scoped>
.div1 {
  border-bottom: 1px solid #eeeeee;
  width: 100%;
  height: 40px;
  padding-top: 18px;
  padding-left: 20px;
  background: #f8fafd;
  font-size: 18px;
  font-weight: bold;
  color: #304156;
  letter-spacing: 1px;
}

.container {
  margin: 20px 20px;
  color: #696a6e;
}

.search-card {
  margin-bottom: 18px;
  border-radius: 10px;
  box-shadow: 0 2px 8px #f0f1f2;
  background: #fff;
}

.search-form {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.bt1 {
  margin-left: 10px;
}

.table-card {
  border-radius: 10px;
  box-shadow: 0 2px 8px #f0f1f2;
  background: #fff;
  padding: 10px 0 0 0;
}

.el-table {
  border-radius: 8px;
  overflow: hidden;
}

.pagination-wrapper {
  display: flex;
  justify-content: center;
  margin: 20px 0 10px 0;
}

/* 保证按钮内容居中 */
.el-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
</style>